package com.zlzlib.liblayoutmanager;

import android.content.Context;
import android.view.View;

/**
 * @Desc: 滑动旋转的类型
 * @Copyright:
 * @DateTime: 2021/8/17 15:05
 * @Author zlz
 * @Version 1.0
 */
public class LayoutRotateManager extends BaseLayoutManager {

    //每一项的间隔
    private int itemSpace;
    //旋转角度
    private float angle;
    //正向旋转还是倒向旋转
    private boolean reverseRotate;

    public LayoutRotateManager(Context context) {
        this(context, 0);
    }

    public LayoutRotateManager(Context context, int itemSpace) {
        this(context, itemSpace, HORIZONTAL);
    }

    public LayoutRotateManager(Context context, int itemSpace, int orientation) {
        super(context, orientation, false);
        this.itemSpace = itemSpace;
        angle = 360f;
        reverseRotate = false;
    }

    @Override
    protected float setInterval() {
        return mDecoratedMeasurement + itemSpace;
    }

    @Override
    protected void setItemViewProperty(View itemView, float targetOffset) {
        itemView.setRotation(calRotation(targetOffset));
    }

    private float calRotation(float targetOffset) {
        final float realAngle = reverseRotate ? angle : -angle;
        return realAngle / mInterval * targetOffset;
    }

    public int getItemSpace() {
        return itemSpace;
    }

    public void setItemSpace(int itemSpace) {
        assertNotInLayoutOrScroll(null);
        if (this.itemSpace == itemSpace) return;
        this.itemSpace = itemSpace;
        removeAllViews();
    }

    public float getAngle() {
        return angle;
    }

    public void setAngle(float angle) {
        assertNotInLayoutOrScroll(null);
        if (this.angle == angle) return;
        this.angle = angle;
        requestLayout();
    }

    public boolean isReverseRotate() {
        return reverseRotate;
    }

    public void setReverseRotate(boolean reverseRotate) {
        assertNotInLayoutOrScroll(null);
        if (this.reverseRotate == reverseRotate) return;
        this.reverseRotate = reverseRotate;
        requestLayout();
    }
}
